<template>
	<view class="route-balance">
		<view class="top">
			<view class="bg">
				<image class="img" src="../../static/images/balance-bg.png" mode=""></image>
			</view>
			<view class="inner">
				<view class="yue">
					余额（元）
				</view>
				<view class="price">
					￥{{formatNumber(yue)}}
				</view>
				<view class="btn" @click="handleGoRecharge">
					去充值
				</view>
			</view>
		</view>
		<view class="footer">
			<u-tabs :current="currentTab" lineColor="#ED2920"
				:itemStyle="{width:'33.33%',height:'44px',background:'#fff'}" :activeStyle="{color:'#ED2920'}"
				:list="list1" @click="handleCheckTab"></u-tabs>
			<view class="cont">
				<view class="none" v-if="!dataList.length">
					<u-empty mode="list"></u-empty>
				</view>
				<view class="have" v-else>
					<view class="cloum" v-for="(item,index) in dataList" :key='index'>
						<view class="left">
							<view class="name">
								{{item.title}}
							</view>
							<view class="time">
								{{item.addtime}}
							</view>
						</view>
						<view class="right">
							<view class="add val">
								<text v-if="item.state==1">+</text>
								<text v-else>-</text>
								{{item.price}}
							</view>
						</view>
					</view>
				</view>

			</view>
			<u-loadmore v-if="dataList.length" :status="loadStatus" />
		</view>
	</view>
</template>

<script setup>
	import dayjs from 'dayjs'
	import formatNumber from '@/utils/formatNumber.js'
	import handleCache from "@/utils/cache.js"
	import getdate from '@/utils/time.js'

	import {
		balanceApi,
		commonApi
	} from '@/api/index.js'
	import {
		ref,

	} from 'vue'
	import {
		onReady,
		onShow,
		onLoad,
		onReachBottom
	} from '@dcloudio/uni-app'

	const list1 = ref([{
			name: '全部',
		}, {
			name: '收入',
		},
		{
			name: '支出',
		}
	])
	const userInfo = ref('')
	const yue = ref(0)
	const currentTab = ref(0)
	const dataList = ref([])
	const currentPage = ref(1)
	const loadStatus = ref('loadmore')
	const totalPage = ref(0)

	onShow(() => {
		userInfo.value = handleCache.get('userInfo')
		console.log(userInfo.value, 2222)
		balanceApi.useryue({
			uid: userInfo.value.id,
			shanghuid: userInfo.value.shanghuid
		}).then((res) => {
			yue.value = res.data.price
		})
		initData()
	})
	onReachBottom(() => {
		loadData()
	})

	const getyue = () => {
		let req = {
			uid: userInfo.value.id,
			shanghuid: userInfo.value.shanghuid
		}
		commonApi.userwallet(req).then((res) => {
			yue.value = res.data.price || 0
		})
	}
	const initData = () => {
		let req = {
			uid: userInfo.value.id,
			shanghuid: userInfo.value.shanghuid,
			state: currentTab.value,
			pageNo: currentPage.value,
			pageSize: 30
		}
		balanceApi.userprice(req, {
			loading: {
				text: '加载中'
			}
		}).then((res) => {
			totalPage.value = res.data.totalPage || 0
			res.data.data.forEach(resp => {
				resp.addtime = getdate.tolocalData(resp.addtime)
			})
			dataList.value = res.data.data || []
			if (totalPage.value > currentPage.value) {
				loadStatus.value = 'loadmore'
			} else {
				loadStatus.value = ' nomore'
			}
			getyue()
		})
	}
	const loadData = () => {
		if (currentPage.value >= totalPage.value) return;
		loadStatus.value = 'loading'
		currentPage.value = ++currentPage.value
		let req = {
			uid: userInfo.value.id,
			shanghuid: userInfo.value.shanghuid,
			state: currentTab.value,
			pageNo: currentPage.value,
			pageSize: 30
		}
		balanceApi.userprice(req, {
			loading: {
				text: '加载中'
			}
		}).then((res) => {
			dataList.value = [...dataList.value, ...res.data.data]
			if (totalPage.value > currentPage.value) {
				loadStatus.value = 'loadmore'
			} else {
				loadStatus.value = ' nomore'
			}
		})

	}
	const handleCheckTab = (data) => {
		currentPage.value = 1
		currentTab.value = data.index
		initData()
	}
	const handleGoRecharge = () => {
		uni.navigateTo({
			url: '/pagesA/recharge/recharge'
		})
	}
</script>

<style lang="scss" scoped>
	.route-balance {
		padding: 30rpx 24rpx 30rpx 24rpx;

		.top {
			position: relative;
			height: 300rpx;

			.bg {
				position: absolute;
				z-index: 1;
				left: 0;
				top: 0;
				width: 100%;
				height: 100%;

				.img {
					display: block;
					width: 100%;
					height: 100%;
				}
			}

			.inner {
				position: relative;
				z-index: 2;
				display: flex;
				flex-direction: column;

				align-items: center;
				width: 100%;
				height: 100%;
				color: #fff;
				padding-top: 53rpx;

				.yue {
					font-size: 28rpx;

				}

				.price {
					margin-top: 10rpx;
					font-size: 60rpx;
					font-weight: 600;
				}

				.btn {
					margin-top: 20rpx;
					width: 209rpx;
					height: 60rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					color: #ED2920;
					border-radius: 30rpx;
					background-color: #fff;
				}
			}
		}

		.footer {
			width: 100%;
			margin-top: 30rpx;

			.cont {
				margin-top: 20rpx;

				.none {}

				.have {
					border-radius: 10rpx;
					background-color: #fff;

					.cloum {
						display: flex;
						align-items: center;
						justify-content: space-between;
						padding: 9rpx 30rpx;

						.left {
							.name {}

							.time {
								margin-top: 4rpx;
								font-size: 20rpx;
								color: #979797;
							}
						}

						.right {
							.reduce {
								color: #ED2920;
							}
						}
					}
				}

			}
		}
	}
</style>